import { List, remove, enterpriseList, add, updateRule, Detail, status } from '../api/deptmanage';
import { message } from 'antd/lib/index';
import checkStatus from '@/utils/status';

export default {
  namespace: 'accountModels',

  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(List, payload);
      yield checkStatus(response);
      yield put({
        type: 'getList',
        payload: response.msg,
      });
    },
    * add({ payload, callback }, { call, put }) {
      const response = yield call(add, payload);
      yield checkStatus(response);

      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    * enterpriseList({ payload, callback }, { call, put }) {
      const response = yield call(enterpriseList, payload);
      yield checkStatus(response);
      yield put({
        type: 'companyList',
        payload: response,
      });
      if (callback) callback();
    },
    * remove({ payload, callback }, { call, put }) {
      const response = yield call(remove, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    * update({ payload, callback }, { call, put }) {
      const response = yield call(updateRule, payload);
      yield checkStatus(response);

      yield put({
        type: 'edit',
        payload: response,
      });
      if (callback) callback();
    },
    * getDesc({ payload, callback }, { call, put }) {
      const response = yield call(Detail, payload);
      yield checkStatus(response);

      yield put({
        type: 'setDesc',
        payload: response.msg,
      });
      if (callback) callback();
    },

    // * getUserList({ payload }, { call, put }) {
    //   const response = yield call(UserList, payload);
    //   yield checkStatus(response);
    //
    //   yield put({
    //     type: 'userList',
    //     payload: response.msg,
    //   });
    // },

    * status({ payload, callback }, { call, put }) {
      const response = yield call(status, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
  },

  reducers: {
    getList(state, action) {
      console.log('action.payload', action.payload);
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        ...state,
        data: action.payload,
      };
    },
    save(state, action) {
      if (action.payload.code === 200) {
        message.success('添加成功');
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload,
      };
    },
    edit(state, action) {
      if (action.payload.code === 200) {
        message.success('更新成功');
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload,
      };
    },

    companyList(state, action) {
      return {
        ...state,
        companyList: action.payload.msg,
      };
    },
    setDesc(state, action) {
      return {
        ...state,
        setDesc: action.payload,
      };
    },
    deptRemove(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    sortChangeReduce(state, action) {
      return {
        ...state,
        data: action.payload,

      };
    },
  },
};
